package com.myproject.mall.common.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
//    交换机
    public static final String ORDER_TOPIC_EXCHANGE = "order.topic.exchange";

//    队列
    public static final String ORDER_CREATE_QUEUE = "order.create.queue";
    public static final String ORDER_PAY_QUEUE = "order.pay.queue";
    public static final String ORDER_CANCEL_QUEUE = "order.cancel.queue";
    public static final String ORDER_TIMEOUT_QUEUE = "order.timeout.queue";
    public static final String ORDER_ALL_QUEUE = "order.all.queue";

//    路由键
    public static final String ORDER_CREATE_ROUTING_KEY = "order.create";
    public static final String ORDER_PAY_ROUTING_KEY = "order.pay";
    public static final String ORDER_CANCEL_ROUTING_KEY = "order.cancel";
    public static final String ORDER_TIMEOUT_ROUTING_KEY = "order.timeout";
    public static final String ORDER_ALL_ROUTING_KEY = "order.*";

//    创建交换机
    @Bean
    public TopicExchange orderTopicExchange() {
        return new TopicExchange(ORDER_TOPIC_EXCHANGE);
    }

//    创建队列
    @Bean
    public Queue orderCreateQueue() {
        return QueueBuilder.durable(ORDER_CREATE_QUEUE).build();
    }
    @Bean
    public Queue orderPayQueue() {
        return QueueBuilder.durable(ORDER_PAY_QUEUE).build();
    }
    @Bean
    public Queue orderCancelQueue() {
        return QueueBuilder.durable(ORDER_CANCEL_QUEUE).build();
    }
    @Bean
    public Queue orderTimeoutQueue() {
        return QueueBuilder.durable(ORDER_TIMEOUT_QUEUE).build();
    }
    @Bean
    public Queue orderAllQueue() {
        return QueueBuilder.durable(ORDER_ALL_QUEUE).build();
    }

//    绑定队列到交换机
    @Bean
    public Binding orderCreateBinding() {
        return BindingBuilder.bind(orderCreateQueue())
                .to(orderTopicExchange())
                .with(ORDER_CREATE_ROUTING_KEY);
    }
    @Bean
    public Binding orderPayBinding() {
        return BindingBuilder.bind(orderPayQueue())
                .to(orderTopicExchange())
                .with(ORDER_PAY_ROUTING_KEY);
    }
    @Bean
    public Binding orderCancelBinding() {
        return BindingBuilder.bind(orderCancelQueue())
                .to(orderTopicExchange())
                .with(ORDER_CANCEL_ROUTING_KEY);
    }
    @Bean
    public Binding orderTimeoutBinding() {
        return BindingBuilder.bind(orderTimeoutQueue())
                .to(orderTopicExchange())
                .with(ORDER_TIMEOUT_ROUTING_KEY);
    }
    @Bean
    public Binding orderAllBinding() {
        return BindingBuilder.bind(orderAllQueue())
                .to(orderTopicExchange())
                .with(ORDER_ALL_ROUTING_KEY);
    }
}
